package com.polaris.live.adapter.home

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.blankj.utilcode.util.StringUtils
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.polaris.live.R
import com.polaris.live.adapter.live.BannerViewHolder
import com.polaris.live.common.adapter.BaseAdapter
import com.polaris.live.common.cacha.UserManager
import com.polaris.live.common.constant.CommonConst
import com.polaris.live.databinding.AdapterCommunityRecommendBinding
import com.polaris.live.databinding.AdapterListBannerBinding
import com.polaris.live.resp.back_resp.DisplayUserBean
import com.polaris.live.resp.back_resp.ListWithBannerBean
import com.polaris.live.translation.R.string
import com.polaris.live.utils.CommonUtils
import com.polaris.live.utils.state.UserStateUtils
import com.polaris.live.widget.refresh.BaseBindingViewHolder

/**
 * CommunityRecommendAdapter
 *
 * @author Created by 半仙 on 2024/9/4/004 20:49
 */
class CommunityRecommendAdapter : BaseAdapter<Any, BaseViewHolder>(0) {

    private var isNeedShowMedal = false

    init {
        addChildClickViewIds(R.id.iv_say_hi, R.id.av_head)
    }

    override fun getDefItemViewType(position: Int): Int {
        return when (val item = data[position]) {
            is DisplayUserBean -> CommonConst.HomeListType.TYPE_NORMAL
            is ListWithBannerBean -> CommonConst.HomeListType.TYPE_BANNER
            else -> throw IllegalArgumentException("Unknown item type: $item")
        }
    }

    override fun onCreateDefViewHolder(
        parent: ViewGroup,
        viewType: Int,
    ): BaseBindingViewHolder<*, *> {
        val layoutInflater = LayoutInflater.from(parent.context)
        return when (viewType) {
            CommonConst.HomeListType.TYPE_BANNER -> BannerViewHolder(
                AdapterListBannerBinding.inflate(layoutInflater, parent, false)
            )

            CommonConst.HomeListType.TYPE_NORMAL -> CommunityRecommendViewHolder(
                AdapterCommunityRecommendBinding.inflate(layoutInflater, parent, false), isNeedShowMedal
            )

            else -> throw IllegalArgumentException("Invalid view type: $viewType")
        }
    }

    class CommunityRecommendViewHolder(binding: AdapterCommunityRecommendBinding, isNeedShowMedal: Boolean) :
        BaseBindingViewHolder<AdapterCommunityRecommendBinding, DisplayUserBean>(binding) {

        private val mIsNeedShow = isNeedShowMedal
        override fun bind(item: DisplayUserBean) {
            mBinding.apply {
                avHead.setImageUrl(item.base.avatar, item.base.userId)
                tvNickName.text = item.base.nickname
                tvNickName.setNicknameColor(
                    item.base.nicknameColor,
                    R.color.color_2e2e2e
                )
                widgetSex.setSexAndAge(item.base.sex, item.base.birthday)
                if (CommonUtils.isMan(UserManager.getUser().sex)) {
                    widgetLevel.setCharmsLevel(item.base.charms)
                } else {
                    widgetLevel.setRichsLevel(item.base.richs)
                }
                tvUserDetail.text = item.base.aboutMe
                userState(mBinding, item)
                if (mIsNeedShow) {
                    widgetMedal.visibility = View.VISIBLE
                    widgetMedal.setMedal(item.medal)
                } else {
                    widgetMedal.visibility = View.GONE
                }
                vipView.setVipLevel(item.base.vipLevel)
            }
        }

        private fun userState(mBinding: AdapterCommunityRecommendBinding, item: DisplayUserBean) {
            val state = UserStateUtils.resolveUserState(item.base)
            if (state == UserStateUtils.USER_STATE_OFFLINE) {
                mBinding.ivState.setBackgroundResource(R.drawable.shape_session_state_offline)
            } else {
                mBinding.ivState.setBackgroundResource(R.drawable.ic_user_space)
            }
            when (state) {
                UserStateUtils.USER_STATE_PARTY_VIDEO, UserStateUtils.USER_STATE_PARTY, UserStateUtils.USER_STATE_GAME -> {
                    mBinding.tvState.visibility = View.VISIBLE
                    when (state) {
                        UserStateUtils.USER_STATE_PARTY, UserStateUtils.USER_STATE_PARTY_VIDEO -> {
                            mBinding.vState.setBackgroundResource(R.drawable.community_recommend_party)
                            mBinding.tvState.text = StringUtils.getString(string.party)
                            mBinding.tvState.setBackgroundResource(R.drawable.community_recommend_text_back_party)
                        }

                        UserStateUtils.USER_STATE_GAME -> {
                            mBinding.vState.setBackgroundResource(R.drawable.community_recommend_game)
                            mBinding.tvState.text = StringUtils.getString(string.diamond_game)
                            mBinding.tvState.setBackgroundResource(R.drawable.community_recommend_text_back_game)
                        }
                    }
                }

                else -> {
                    mBinding.vState.setBackgroundResource(0)
                    mBinding.tvState.visibility = View.GONE
                }
            }
        }

    }

    override fun convert(holder: BaseViewHolder, item: Any) {
        when (holder) {
            is CommunityRecommendViewHolder -> if (item is DisplayUserBean) holder.bind(item)
            is BannerViewHolder -> if (item is ListWithBannerBean) holder.bind(item)
        }
    }

    fun setNeedShowMedal(isShow: Boolean) {
        isNeedShowMedal = isShow
        notifyDataSetChanged()
    }
}